//
//  PaymethodTableViewCell.m
//  ChunLaiZhuangClient
//
//  Created by xielihui on 16/4/12.
//  Copyright © 2016年 CLZ. All rights reserved.
//

#import "PaymethodTableViewCell.h"
#import "CheckoutModel.h"

@interface PaymethodTableViewCell()
@property (unsafe_unretained, nonatomic) IBOutlet UIImageView *iconImageView;
@property (unsafe_unretained, nonatomic) IBOutlet UILabel *titleLabel;
@property (unsafe_unretained, nonatomic) IBOutlet UIButton *selectBtn;
@property(nonatomic, strong) PaymentMethodModel *payMethodModel;
@end

@implementation PaymethodTableViewCell

- (void)awakeFromNib {
    // Initialization code
}

- (void)updatePaymentInfo:(PaymentMethodModel *)model {
    self.payMethodModel = model;
    [self.iconImageView sd_setImageWithURL:[NSURL URLWithString:model.icon]];
    self.titleLabel.text = model.title;
    [self setBtnImages];
    [self.payMethodModel addObserver:self forKeyPath:@"selected" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil];
    
}

- (IBAction)selectBtnAction:(id)sender {
    self.payMethodModel.selected = YES;
    if (self.selectPayMethodBlock) {
        self.selectPayMethodBlock(self.payMethodModel);
    }
}

- (void)setBtnImages {
    if (self.payMethodModel.selected) {
        [self.selectBtn setImage:[UIImage imageNamed:@"btn_select_selectd"] forState:UIControlStateNormal];
    } else {
        [self.selectBtn setImage:[UIImage imageNamed:@"btn_select_nor"] forState:UIControlStateNormal];
    }
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context {
    if ([keyPath isEqualToString:@"selected"]) {
        [self setBtnImages];
    }
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)dealloc {
    [self.payMethodModel removeObserver:self forKeyPath:@"selected"];
}

@end
